<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/10/19
 * Time: 16:16
 */

namespace app\index\table;

use app\index\controller\Login;
use app\index\service\ExcelService;
use think\Db;

class IntangibleTable extends BaseTable
{
    protected static $view = 'V_INTANGIBLE_INFO';
    protected static $table = 'INTANGIBLE_INFO';
    protected static $pk = 'INTANG_NO';

    static public function table(){
        return Db::table(static::$view);
    }

    static public function add($data){
        $data['INTANG_NO'] = static::getNextId();
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');
        self::toDate($data,'GET_DATE');
        self::toDate($data,'ENTER_TIME');
        self::toDate($data,'REGISTER_DATE');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }

    static public function addImport($data){
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');
        self::toDate($data,'GET_DATE');
        self::toDate($data,'ENTER_TIME');
        self::toDate($data,'REGISTER_DATE');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }

    static public function update($where,$data){
        self::toDate($data,'GET_DATE');
        self::toDate($data,'ENTER_TIME');
        self::toDate($data,'REGISTER_DATE');
        Db::table(static::$table)->where($where)->update($data);
    }

    static public function delete($where){
        return Db::table(static::$table)->where($where)->delete();
    }

//    private static function toDate(&$data,$key){
//        if(array_key_exists($key,$data)){
//            $value = $data[$key];
//            if(empty($value)){
//                $data[$key] = null;
//            }
//            else{
//                $data[$key] = Db::raw("to_date('$data[$key]','yyyy-mm-dd hh24:mi:ss')");
//            }
//        }
//    }

    public static function
    getConfigAdd(){
        return Db::table('INTANGIBLE_CONFIG')->where(['ADD_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigEdit(){
        return Db::table('INTANGIBLE_CONFIG')->where(['EDIT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigPrint(){
        return Db::table('INTANGIBLE_CONFIG')->where(['PRINT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getNextId(){
        $year = date('Y');
        $numMaxRecord = Db::table(static::$table)
            ->field('max(SUBSTR(INTANG_NO,5,4)) as NUM')
            ->where([['SUBSTR(INTANG_NO,1,4)','=',$year]])
            ->fetchSql(true)
            ->find();
        $numMaxRecord = Db::query($numMaxRecord);
        $num = $numMaxRecord[0]['NUM'];
        if ($num == NULL) {
            $num = 0;
        }
        $num++;
        return $year.sprintf("%04d", $num);
    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'资产编号','field'=>'INTANG_NO']
            ,['title'=>'资产名称','field'=>'INTANG_NAME']
            ,['title'=>'分类号','field'=>'ASSET_TYPE_ID']
            ,['title'=>'分类名称','field'=>'CNAME94']
            ,['title'=>'所在学院','field'=>'COLLEGE_NAME']
            ,['title'=>'使用部门','field'=>'DEPART_NAME']
            ,['title'=>'取得方式','field'=>'GETTYPE']
            ,['title'=>'取得日期','field'=>'GET_DATE']
            ,['title'=>'入账日期','field'=>'ENTER_TIME']
            ,['title'=>'使用方向','field'=>'USEDIRECTION']
            ,['title'=>'状态','field'=>'STATUS']
            ,['title'=>'价值','field'=>'VALUE']
            ,['title'=>'评估价值','field'=>'EVAL_VALUE']
            ,['title'=>'注册登记机关','field'=>'REGISTER_OFFICE']
            ,['title'=>'注册登记日期','field'=>'REGISTER_DATE']
            ,['title'=>'专利号','field'=>'PATENT_NO']
            ,['title'=>'批准文号','field'=>'APPROVAL_NO']
            ,['title'=>'制单人','field'=>'INPUT_NAME']
            ,['title'=>'制单时间','field'=>'INPUT_TIME']
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }

    const INTANGIBLE_FIELD_LIST = [
        "INTANG_NO","INTANG_NAME","ASSET_TYPE_ID","DEPART_ID","GETTYPE_ID","GET_DATE","ENTER_TIME","USEDIRECTION_ID","STATUS_ID","VALUE","EVAL_VALUE","REGISTER_OFFICE","REGISTER_DATE","PATENT_NO","APPROVAL_NO"
    ];
}